package com.example.cgq.business;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.cgq.common.CellPhoneInfo;
import com.cgq.common.CommonFun;
import com.com.cgq.data.exchenge.ListData;
import com.com.cgq.data.exchenge.ListDataAdapter;
import com.com.cgq.data.exchenge.ListDataMember;
import com.com.cgq.data.exchenge.MainViewPage;
import com.com.cgq.data.exchenge.SettingDataToFile;
import com.side.view.SideMenu;

import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity  {

    private SideMenu sideMenu;
    private ListView mlistViewContentData;
    private SettingDataToFile mSettingData;
    private ViewPager mViewPager;
    private CommonFun mCommonFun;
    private CellPhoneInfo mCellPhoneInfo;
    private ListData mListData;
    private MainViewPage mMainViewPage;
    private static String URL="http://www.imooc.com/api/teacher?type=4&num=30";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar=getSupportActionBar();
        actionBar.hide();
        setContentView(R.layout.activity_main);
        initWeight();
        //SetContentListView();
        checkSide();
        mMainViewPage.initPageView();
        //CheckIsTochInViewPage();

        new FoodAsynTask().execute(URL);//FoodAsynTask将一个url传递进来，实现对url的异步访问

    }

    @Override
    protected void onResume() {
        boolean bIsShowViewPage =  mSettingData.getIsShowViewPage();
        mMainViewPage.setIsHideViewPage(bIsShowViewPage);
        super.onResume();
    }

    private void CheckIsTochInViewPage() {
        //sideMenu.onInterceptHoverEvent(MotionEvent ev)

    }

    public  void checkSide() {
       sideMenu.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               if (MotionEvent.ACTION_MOVE == event.getAction()) {
                   int  mCurrentPosX = (int)event.getX();
                   //int mCurrentPosY = (int)event.getY();

                  /* Log.i("tag", mCurrentPosX+"qww");*/

                   boolean bIsCanLeftDrag = false;
                   bIsCanLeftDrag =  mSettingData.getIscanLeftDrag();
                   //Log.i("tag", bIsCanLeftDrag+"main");
                   if ( !bIsCanLeftDrag && (mCurrentPosX <= 10000) &&(!sideMenu.getIsOpen())) {
                       sideMenu.MoveToleftSide();
                       return true;
                   }
               }

               //如果打开点一次关闭
               if (MotionEvent.ACTION_DOWN == event.getAction()) {
                   if (sideMenu.getIsOpen()) {
                       sideMenu.MoveToleftSide();
                   }
               }

               return false;
           }
       });
    }

    public  void initWeight() {
        GetDataFormOtherActivity();
        sideMenu = (SideMenu) findViewById(R.id.id_SideMenu);
        mlistViewContentData = (ListView) findViewById(R.id.id_ContentListView);
        mSettingData = new SettingDataToFile(MainActivity.this);
        mCommonFun = new CommonFun();
        mListData = new ListData();
        mViewPager = (ViewPager) findViewById(R.id.id_main_viewpage);
        mMainViewPage = new MainViewPage(MainActivity.this, mViewPager,mCellPhoneInfo);
        mCellPhoneInfo = new CellPhoneInfo();
    }

    private void GetDataFormOtherActivity() {
        Intent intent = MainActivity.this.getIntent();
        if (intent != null) {
            mCellPhoneInfo = (CellPhoneInfo) intent.getSerializableExtra("CellPhoneInfo");
        }
    }

    public void SetContentListView() {
        ContentListView contentListView = new ContentListView();
        List<Map<String,Object>>dataLists;
        SimpleAdapter m_SimpleAdapter;
        dataLists = contentListView.getDateList();

//        m_SimpleAdapter = new SimpleAdapter(this,dataLists,R.layout.content_list,new String []{"pic","text"},new int[]{R.id.pic,R.id.text});
//        mlistViewContentData.setAdapter(m_SimpleAdapter);

    }

    // menu button
    public void onChange(View view) {
        sideMenu.toggle();
    }

    public void onClickSettingBtn (View view) {
        Intent intent = new Intent(MainActivity.this, Setting.class);
        startActivity(intent);
    }

    // menu key
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        sideMenu.toggle();
        return true;
    }

    //back key
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (sideMenu.getIsOpen()) {
                sideMenu.toggle();
            }

            else{
                finish();
            }
        }

        return true;
    }

    //实现网络的异步访问
    class FoodAsynTask extends AsyncTask<String, Void, List<ListDataMember>> {//Void 表示不记录中间过程
        protected List<ListDataMember> doInBackground(String... param) {
            return mListData.getJsonData(param[0]);
        }
        //第三步 onPostExecute将生成的数据传递给NewsAdapter 即将生成的listDataMember设置给listview
        protected void onPostExecute(List<ListDataMember> listDataMember) {
            super.onPostExecute(listDataMember);
            ListDataAdapter adapter=new ListDataAdapter(MainActivity.this,listDataMember,mlistViewContentData);
            mlistViewContentData.setAdapter(adapter);
        }
    }

}
